package com.zsrt.java.day20161227.zzd.demo1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class Qianghongbao2 {
	//已抢的红包数
	private int s=0;
	//红包总数
    private int a=0;
    //红包钱数
    private double money;
    //红包随机分配的list
    private List<Double> array=new ArrayList<>();
    public Qianghongbao2(int a,double money){
        this.a=a;
        this.money=money;
        Random r=new Random();
        double sum=0;
        for (int i=0;i<a;i++){
        	//经过小小的计算，这样使最小的钱尽量接近0.01；a越大，此计算越没有用
            array.add(r.nextDouble()*money+0.01*a*money);
            sum+=array.get(i);
        }
        for(int i=0;i<array.size();i++) {
            array.set(i,array.get(i)/sum*money);
        }
        //double z=1.0*money;
        Collections.sort(array);
        //将钱进行分配；
        for(int i= 0;i<array.size();i++){
        	//不足0.01的直接给0.01；
            if(array.get(i)<=0.01){
                array.set(i, 0.01);
            }else{
                array.set(i, (int)(array.get(i)*100)*1.0/100);
                }
                Collections.shuffle(array);
           }
    }
    public void getBag(String name){
            if(array.get(s)<=money){
              System.out.println(name+"抢了"+" "+array.get(s)+"元");
              s++;
            }
    }
    public static void main(String[] args){
    	Qianghongbao2 lb=new Qianghongbao2(10,1.0);
        String[] name={"帅哥1","帅哥2","帅哥3","帅哥4","帅哥5","帅哥6","帅哥7","帅哥8","帅哥9","帅哥10"};
        System.out.println("红包一共"+lb.money+"元"+"   "+"分成了"+lb.a+"个红包");
        for(int i=0;i<name.length;i++){
            lb.getBag(name[i]);
        }
    }

}
